package org.swimv2.servlet;

import java.io.IOException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.swimv2.controller.GestioneAbilita;
import org.swimv2.model.Persona;

/**
 * Servlet implementation class PaginaProfiloServlet
 */
public class FotoProfiloServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FotoProfiloServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        Properties props = new Properties();
		props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
		props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
		props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
		Context ctx;
		try{
			ctx = new InitialContext(props);
			Object obj = ctx.lookup(GestioneAbilita.JNDI_NAME);
		
			Persona u = (Persona) request.getSession().getAttribute("utente");
			
		
			byte[] imageData = u.getFoto();
			if(imageData!=null){
				response.setContentType("image/jpg");
				response.getOutputStream().write(imageData);
			}
			else{				
				byte[] imageInByte= "ffd8ffdb004300080606070605080707070909080a0c140d0c0b0b0c1912130f141d1a1f1e1d1a1c1c20242e2720222c231c1c2837292c30313434341f27393d38323c2e333432ffdb0043010909090c0b0c180d0d1832211c213232323232323232323232323232323232323232323232323232323232323232323232323232323232323232323232323232ffc00014080060006004012200021101031101042200ffc4001f0000010501010101010100000000000000000102030405060708090a0bffc400b5100002010303020403050504040000017d01020300041105122131410613516107227114328191a1082342b1c11552d1f02433627282090a161718191a25262728292a3435363738393a434445464748494a535455565758595a636465666768696a737475767778797a838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae1e2e3e4e5e6e7e8e9eaf1f2f3f4f5f6f7f8f9faffc4001f0100030101010101010101010000000000000102030405060708090a0bffc400b51100020102040403040705040400010277000102031104052131061241510761711322328108144291a1b1c109233352f0156272d10a162434e125f11718191a262728292a35363738393a434445464748494a535455565758595a636465666768696a737475767778797a82838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae2e3e4e5e6e7e8e9eaf2f3f4f5f6f7f8f9faffda000e040100021103110400003f00f7fa28a2803e7fa28a28a28a2800a28a2b3b55d52cb44d32e351d4ee52dacedd37c92bf451fcc9270001c92401926bc2757f8ade2fd76fda6d12e22d134c563e4ab4092cd2a1c60befca83c1202e31bb04b601aee3e39cf3a781ad2d22936c57ba94104ebb41de9867c73d3e6453c73c7a66bcc3ecb34f716d61a64301b99c16124c3e48d0632c71d7a8007a9ac6a4dc7489d387a5195e5226b7b796eae22b782369269582468a325989c003f1afa1bc1ff0074a16315cf89279aeae5d72d0db49b228cfa6e1cb1f7181f5eb5e65f072c22bef89164d282c2d6296e1463f8954edfc8907f0afaf23da6342872b81823b8aa1a478bbc53e16f145c7887ed6da9b5db86bf8a4623ce51d88e8b81f74a8f97a01b72a7e9ad1359b3d7f44b2d5ac1f7db5dc4254c904ae7aab6090181c8233c10457ce92d85ddb6a0b61aac31c576e09b79e0cf9770075033d187a1fc2b7be18df5ef877c7f0e8d0bbbe97ac24bba1dd858a7442fe60183c9542a40c67209ced02a69d46df2c8aaf4528f3c0f20f147c08d1ee741920f0fbb5addc6c6487ed0e5d7a7299ea01e3939af9b6fec2eb4bbf9ac6f61782ea0729244e30548afbd31918ed5e13fb4078521974c87c470c6ab716cc90ccc3ac913640cfa956c0cfa37b57d0145145741c87cef4514514514500145145274aa97f35c5be9d73359db7daeea389da1b7f30279ae012a9b8f0b938193d334b7f79069d61737b72fe5c16d134d2bed2db514124e0727807815e01adfc4bd77c57aebb68b77a8e9da281b2311c691b64c65599e405b272cd8dad81b50e030cd2949455d9508393b20a70c16193819e4d3a38a49a558a352eeec15540c924f415ed7e0cf81773756f0de789565b47773b6d7209da0704e3207d091599ad78f7c737d35fdb6ade218f43512989ed6c5510c6f19c3059012e3e6539c39ce4f638ae66f352d4b53b692cee7c69a95dc12637417176f223e08232a5b07900fe156aced2d631e5e9ba2ace1723ed57402a9f719058fe02b6d34eb4110492d2d99b1f31f28609efdab9a5575dceda74236dbf335fe1d7c2af066bfa2c7a94d69a8dec4df765b998441c8eb848cfca3eac4d774df063c0471b344f2d87464b9941ff00d0abb0d36c974eb28ad11b2912e155515401d800a0018e957727762b9d8b4bd46f6489aeae84ed6e8896d23c8e4c48872b180d9dabd700703d2baeb795ede4f3502aca23f28381f36cce71f9d3238d23454450a8a3000e80543777b6d611092e64da18ed5014b163e800e4d60e4e474c61182d0e2347f863e1ef0d6a4fa868d03c33c9b95833ee014a9042fa75fd2bb1b6568e2546e31c0f7152ed19ce29d8abf71752dddb2dbdc9f3a34759137fde56072083d41aa1a9dbbea003a4c60b94944d14c990d1b83905482083ee0d2da5edb5f41e75acab2479c1c7507d08ec6ac52bb4cbb464bd42b1fc47e1bd33c55a449a5eab13496ce41211ca9041c8c115b1d28ae8acbe2ef88f45551aee956faa5aaf96ad73624c332a8fbeec872acc7ae06c19cf4078f49f0af8ebc3de31b7dfa46a28f384dd25ac9f24f170b9ca1e480580dc32b9e0135e23323490ba2384623018aeec7e1deb98bbd3eeec2f06a36e26b2bc8f256f74d765232083950772e4120ed27a9ae9a75afa48e1ad86b6b03e71f167c00934e8a4bad1356df128cf977abb71ff006d1781f8803debc8b57d0f53d06e05bea76525bbb0dc858655c7aab0e187b82457dd2ca1d4ab00548c10464115c57883e1ed8ea504e2ce38e35949696c665df6d29f5d99fddb7fb6854fd6bebba5af2df855f11af7c592dd691abb5a49a85b42b343716c081711f0accc3a2b06db9c633bf851b4d7a9574a7738dab687c714575de3ef0aff00c225e216b38e39121750ea9236ed99ec1b8c8f42403fccf235e5ff001cccbff08459f338b33a9c22f4c6582f9387fbf8fe1dfb3af19dbdf15e716cda443aa5945ab98974d689c44ae710f9a08dbbbb7ddce33c75afa1b55d2ecb5cd2ee34ed4ed92e6cee1364913f461fcc1070411c82011822bc5f52f843e23d192687489ed75bd2c0fdd5add623b85cb101013f23003049257383851c039548396a8de85451ba677ff06859b7c50d2fed98ce24306ee9e6ec3b7ffadef8afaf63c6dfe55f055bcd2db4f1cf048d1cb1b07474382ac39041ec6bdc740fda1de0d1960d6f4b92e7508c6d17303285907f7994f43eb8e0fb5665dc904cfb6d5621029c4462c105474e9586faf5ac6edf25c18518abdc2c24c408e0fcdfd6b7efb40d5b418a18350d39ad81b7490c90233c109625446d205d81c1c02012391827358da330fec4b656017cb431b83d015241fd41ae571e5f891dca7cf6e567d0724624001ea0e452057500e412072077af3af03fc61d0bc5f32d8cff00f12ed49df6c70ccc36cbe9b5bd71d8f7e99af49041008e41abb1c892a2bc6c1918654839047ad52233e2106453f2dafee4e38ceef9ff001c6dfc2ab417fa3e9eef6f6d71bf7b9610c2a65d84f500283819ed538d60383e4e99aacbce3e4b46feb4b95f44573c5eec4520f038f634b49b46edd8e7d69692f6d5adee9350b343e76e559e341feb909c723d46720d69d64dc5eeacef1476da2ea302b7fac9ee2c9db67d157935d0d8f832eafada39e6f115e2ef190b1d9ac247e0c323f1a6e2eda894d5df2a0edeb40e714801ee453aaa5157a4f05081c47ff095dd248dc2acab1127f03d6a86a7a0eb3a05b35f3ea31ea3651106643008e455eec307071d71e953cbd995cfdd09452d20ae6f559af349bb92f2c65b9d3aee5531a5d584cd1198120ec72a473c03ef815f507876e27bbf0c6937373749773cf670c925c46a556562809700852012738c0ebd074af009a186eedda2951648641c83d08ae97e1e78e6e7c37a9d978535a924b8d36e9d60d32f082cf03938581fb95c9014ff000f4fbbf73a28544fdd671e2a935efad8e1be25e8d6ba9f87e592f3441aa4512121138990ff00791baafea3d457c8170aab712ac618207214375c678cfbd7df0466be6ef8cdf0b5747797c4ba1438b273baf2d507fa924fdf51fdc24f23b13e9d3dc68a28ae9388f12a28a2bc67e25f8e24d4ae6efc27a116b830ec17f25b4bcb92595adfeef03250b306e06e52386158763e0cb54b54b9f12dcacfb00221326cb78bf96f3eec7f0a961b08ecbe306bb62eeb2b4324f781826dc19fcb93919e481215cfe3c6715cf78d342d662d7e4d5bec3fdad627948f993c9e3a18f3d3dc0ae59b6e76bd8eea49469f32573d23e1078222f17f889e4ba6436b67879223d5c1cf23e871f9d7d6304220823854b32a285058e4903d6bc8ff00679ba4b9f075ea08d16486e423328c161b72335ec55d745e27f07e949e4dbea5a6dba0e36418c0ff00be456be9fab69fab44d269f7d05d221c3189f3b4fbfa578cdb7882fef6e96c34bd22dc5d3e556148c82bf5e8063debd43c27e1d9342b59a5bc9c4f7f7455a7655daab81c2a8f6c9e7bd44e118af335a552727b681451495d166b8bf8977b7563e1e89ed75092d5e49c2148ced69411c80dd463af15da019af17f889ae0d63c426d6070d6b6398948e4349fc6dfa05fc0d443e234a8fddb0b49452d64f85f598b43d5d6f2e6da2bc8cb03299e30f220fefa39e411d71debdbf57115c6837a72af13db3b03d882a4e6bc1acacd6eb871fbb5383838dc71c827d07f8d7ac5ddd3da7c2eb75663e7cf6315bc79ea59c051fa1aa93e67e63f632a50527b4aed7c8292968ac0d30b1d26ccb7de302673fee8a5d42ca1bfb196da75dc8e3f107b1153a208e358d7eea80a3e8295bee9fa5657d6e8bb69662557bb823b8b59a29515d1e365656190c08e411e95629b27fab6fa1afa4e8a2b17c4daf41e19f0edfeb3700ba5ac7b95324798e48544c8071b98a8ce38ce4f15e99e21f01d1454b6f0b5c5cc5027de91c20fa938af26f153e85a1fc6983ec724504f7d6c56fa24c05599c96563f370cdb572303ef2b725ce3a6e6bc46c204d666b8bbbbb55ba9ae65796e6f18050f23124ec000ee73c600ed5b716abe27d1620b69a8adddb28c2a5ec7bca8f4dc39fceb8eab5291e950528c3b9ef3fb38ea9b24d634c96551e60496142dc9db9dd81e9f30afa02bce7e1bfc3bb0f0ee8f65773d8c69aa81bbceeae0723af6c83c8af46ed5e9e6188ce2630c666190242837007dfad498af356f897aa5ac4ad75a45a4996da5e29dc05f72369c0f7a83c45e3cd75f4c06c6d22b285fef5edbcc2e723d1594610fae79f4a8f6727b1a3ab141452d15bbe3af1947a2dac9a6d84a1b539570c579fb3a9fe23fed7a0fc6bc7e389c8089c12c1771ec49ff2695564989750f2b312c5f3b8b1ee49ee6ba0f0af87b50d6f5048ed5fc98108966b8dbb8478e806782dc9e3b77ab4ac4277776145252d68f873426d5ef469d0874b78c0fb54a07dc43fc39fefb7e8326ba8f12de477bad5b6996f8fb369a049285e82423089f82e4fe22adbce2c631e1bf0a46ad7287fd26e9be64833d5e46fe290fa75fa0a6dd787ad745d263585d9df7e649643f348e792c7dcd676e5474d5aceb4aef45dba24b641494507a62b2e91bee9fa528a46fba7e950014d93fd5bff00ba69c083dfa5364ff56dfee9afa4abc3fe2af89bfe123d6078474f9d5b4fb46136a53c32365a40481071c60753d7e603ee9439ed7e2678e7fe10bd16316e864d42fbcc8e0f9b1e5054e651956562acd18da71bb77b1af1bd36ddedad4b5ccad2ddcec66b995dcb33c8dc9249e4fd4d76d6a9cab4dcf2f0f4b9e577b23e03af6cf817f0fc6a5a82f8ab518c1b5b57c59c67fe5a4a3ab9ff00657b7a9fa5799f83bc373f8b3c4d67a5438559641e639380a83ef1fae2beced334fb2d1f4cb7d3ec5123b6b78d638d41e8a3fce7eb57238d228d638d02220c2a8180052d1b87a8fce8c8f51f9d711ea1a14537cc4fefafe747989fdf5fcea9ddd8c52a1755dafeaa3ad54d1b4092f3501f671716a49c35cdab18c8fa91c1fc735ac5873961f9d769a1c11dae9710054161b8e3de9a6d6c2924d598ea29be627f7d7f3a3cc4fefafe75cd1f076a367299618f41d549393f6eb111487fe049c1fc45694761afdec3f63bc9acf4bb3fba574e2cd238feeab10020f7009fa5747e6291c30fce90b29c1dc33f5aa736ccd534b61f494df313fbebf9d1e627f7d7f3a8ac2c2d34cb34b4b1b748204e88a3bf724f727d4d6578a2402da08f3cb396c7d07ff5eb6bcc5df8dc3a67ad739e286cdc5b9de08d870b9f7eb50cd10ea29be627f7d7f3a3cc4fefafe758348df74fd28dc3d47e748586d3c8e9eb40c75365ff0054ff00ee9a3cc4fefafe74d9244f2dfe75e87bd7ffd9".getBytes();
				
				response.setContentType("image/jpg");
				response.getOutputStream().write(imageInByte);
			}

		}catch(NamingException e){
			e.printStackTrace();}	
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
